local sk__miluan = fk.CreateSkill {

  name = "sk__miluan",

  tags = {  },

}



sk__miluan:addEffect(fk.Damaged, {
  name = "sk__miluan",
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__miluan.name) and
      table.find(player.room:getOtherPlayers(player), function (p) return not p:isKongcheng() end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, table.map(room:getOtherPlayers(player), Util.IdMapper))
    local moves = {}
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not p:isKongcheng() then
        table.insert(moves, {
          ids = p:getCardIds("h"),
          from = p,
          to = player,
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonPrey,
          skillName = sk__miluan.name,
          proposer = player,
        })
      end
    end
    room:moveCards(table.unpack(moves))
    if player.dead or player:getHandcardNum() < 2 or #room:getOtherPlayers(player) == 0 then return end
    room:delay(1000)
    math.randomseed(tonumber(tostring(os.time()):reverse():sub(1, 6)))
    local targets = room:getOtherPlayers(player)
    local cards = table.random(player:getCardIds("h"), player:getHandcardNum() // 2)
    local id_mapper = {}
    for _, id in ipairs(cards) do
      local n = math.random(#targets)
      id_mapper[n] = id_mapper[n] or {}
      table.insert(id_mapper[n], id)
    end
    moves = {}
    for i = 1, #targets, 1 do
      if id_mapper[i] then
        table.insert(moves, {
          ids = id_mapper[i],
          from = player,
          to = targets[i],
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonGive,
          skillName = sk__miluan.name,
          proposer = player,
        })
      end
    end
    room:moveCards(table.unpack(moves))
  end,
})

return sk__miluan